package com.mszlu.news.controller;

import com.mszlu.news.common.aop.LogAnnotation;
import com.mszlu.news.common.cache.Cache;
import com.mszlu.news.service.ArticleService;
import com.mszlu.news.vo.Result;
import com.mszlu.news.vo.params.ArticleParam;
import com.mszlu.news.vo.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据进行交互
@Api(tags = "文章查询")
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @ApiOperation(value = "文章列表")
    @PostMapping
    //加上此注解 代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
//    @Cache(expire = 5 * 60 * 1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     * @return
     */
    @ApiOperation(value = "最热文章")
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @ApiOperation(value = "最新文章")
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000,name = "news_article")
    public Result newArticles(){
        int limit = 5;
        return articleService.newArticles(limit);
    }

    @ApiOperation(value = "类别热门文章")
    @PostMapping("categoryhot/{categoryId}")
    public Result hotArticleByCategory(@PathVariable("categoryId") Long categoryId){
        return articleService.hotArticleByCategory(categoryId);
    }

    @ApiOperation(value = "标签热门文章")
    @PostMapping("taghot/{tagId}")
    public Result hotArticleByTag(@PathVariable("tagId") Long tagId){
        return articleService.hotArticleByTag(tagId);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @ApiOperation(value = "月资讯")
    @PostMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    @ApiOperation(value = "轮播图1")
    @PostMapping("carousel")
    public Result carousel(){
        int limit = 5;
        return articleService.carousel(limit);
    }

    @ApiOperation(value = "轮播图2")
    @PostMapping("secondCarousel")
    public Result secondCarousel(){
        int limit = 5;
        return articleService.secondCarousel(limit);
    }

    @ApiOperation(value = "通过id查找文章")
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }
    //接口url：/articles/publish
    //
    //请求方式：POST
    @ApiOperation(value = "发布文章")
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @ApiOperation(value = "更新文章")
    @PostMapping("updateArticle")
    public Result updateArticle(@RequestBody ArticleParam articleParam){
        return articleService.updateArticle(articleParam);
    }

    //通过作者id找出文章
    @ApiOperation(value = "通过作者id找出文章")
    @PostMapping("view1/{authorId}")
    public Result findArticleByAuthorId(@PathVariable("authorId") Long authorId){
        return articleService.findArticleByAuthorId(authorId);
    }

    @ApiOperation(value = "搜索框查询")
    @PostMapping("select")
    public Result selectArticle(@RequestBody ArticleParam articleParam){
        return articleService.selectArticle(articleParam);
    }

    @ApiOperation(value = "通过id删除文章及其关联的表数据")
    @PostMapping("delete/{id}")
    public Result deleteArticleById(@PathVariable("id") Long articleId){
        return articleService.deleteArticleById(articleId);
    }
}

